//
//  CHSViewController.m
//  CAIAvoidCrash
//
//  Created by 海参 on 12/07/2021.
//  Copyright (c) 2021 海参. All rights reserved.
//

#import "CHSViewController.h"
#import <ZXMBProgressHUD/ZXMBProgressHUD.h>
#import <ZXCategoriesUtil/CAICategories.h>
#import "CHSHeaderTitleView.h"
#import "CHSArrayCrashController.h"
#import "CHSDictionaryCrashController.h"

@interface CHSViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMapTable *mapTable;

@property (nonatomic, copy) NSArray *dataArray;

@end

@implementation CHSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUI];
    [self setData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUI{

    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    [self.tableView registerClass:[CHSHeaderTitleView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([CHSHeaderTitleView class])];
}

#pragma mark - Data

- (void)setData
{
    //组数据
    NSArray *sectionArr = @[
        @{
            @"section":@"Array",
            @"sectionTitle":@"有序的线性表，对象可重复",
            @"list":@[
              @{
                  @"tag":@"Array",
                  @"title":@"NSArray&NSMutableArray(有案例)"
              },
        ]},
        @{
            @"section":@"NSPointerArray",
            @"sectionTitle":@"类似NSArray的有序的线性表，主要是可弱引用对象",
            @"list":@[
              @{
                  @"tag":@"NSPointerArray",
                  @"title":@"NSPointerArray"
              },
        ]},
        @{
            @"section":@"Dictionary",
            @"sectionTitle":@"无序的哈希表（key-value形式，key是唯一的）",
            @"list":@[
              @{
                  @"tag":@"Dictionary",
                  @"title":@"NSDictionary&NSMutableDictionary(有案例)"
              },
        ]},
        @{
            @"section":@"NSMapTable",
            @"sectionTitle":@"根据NSDictionary建模的哈希表，主要是可弱引用对象",
            @"list":@[
              @{
                  @"tag":@"NSMapTable",
                  @"title":@"NSMapTable"
              },
        ]},
        @{
            @"section":@"Set",
            @"sectionTitle":@"无序的哈希表（不需有序时可用于替代NSArray，哈希表的速度更快，不可重复",
            @"list":@[
              @{
                  @"tag":@"Set",
                  @"title":@"NSSet/NSMutableSet"
              },
        ]},
        @{
            @"section":@"NSCountedSet",
            @"sectionTitle":@"继承于NSMutableSet，插入到NSCountedSet对象中的每个不同对象都有一个与之关联的计数器。",
            @"list":@[
              @{
                  @"tag":@"NSCountedSet",
                  @"title":@"NSCountedSet"
              },
        ]},
        @{
            @"section":@"NSHashTable",
            @"sectionTitle":@"根据NSSet建模的哈希表，主要是可弱引用对象。",
            @"list":@[
              @{
                  @"tag":@"NSHashTable",
                  @"title":@"NSHashTable"
              },
        ]},
        @{
            @"section":@"NSIndexSet",
            @"sectionTitle":@"索引集，是一个唯一的（index不可重复），有序的（添加的index会按index大小在集合内部自己排序），无符号整数的集合。主要用于tableView/collectionView的索引管理。",
            @"list":@[
              @{
                  @"tag":@"NSIndexSet",
                  @"title":@"NSIndexSet/NSMutableIndexSet"
              },
        ]},
        @{
            @"section":@"KVC",
            @"sectionTitle":@"KVC崩溃",
            @"list":@[
              @{
                  @"tag":@"KVC",
                  @"title":@"案例：KVC崩溃"
              },
        ]},
    ];
    self.dataArray = sectionArr;

}


#pragma mark -


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    NSArray *list = [dic objectForKey:@"list"];
    return list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = [list objectAtIndex:indexPath.row];
    NSString *value = [rowDic objectForKey:@"title"];
    cell.textLabel.text = value;
    cell.textLabel.font = [UIFont zx_systemFontOfSize:14];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CHSHeaderTitleView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([CHSHeaderTitleView class])];
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    headerView.titleLab.text = [NSString stringWithFormat:@"%@.%@",@(section+1),[dic objectForKey:@"sectionTitle"]];
    return headerView;
}

///不支持titleLabel换行显示
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
//}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = list[indexPath.row];
    NSString *tag = [rowDic objectForKey:@"tag"];
    if ([tag isEqualToString:@"Array"]) {
        [self.navigationController zx_pushStoryboardViewControllerWithStoryboardName:@"Main" identifier:NSStringFromClass([CHSArrayCrashController class]) withData:nil];
    }
    if ([tag isEqualToString:@"Dictionary"]) {
        [self.navigationController zx_pushStoryboardViewControllerWithStoryboardName:@"Main" identifier:NSStringFromClass([CHSDictionaryCrashController class]) withData:nil];
    }
    if ([tag isEqualToString:@"KVC"]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"key":@"value"}];
        [dic setValue:@"" forKey:nil];
        
//        [self setValue:@"" forKey:nil];
//        id array = [self valueForKey:@"keyValueCodingArray"];
    }
}



- (IBAction)rightBarItemAction:(id)sender {
     [self hashTableExample];
}





- (void)mdictionaryCrashSubscript{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"key"];
    id obj = nil;
    dic[obj]=@"2";
}

- (void)mdictionaryCrashRemove{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"key"];
    [dic removeObjectForKey:nil];
}

- (void)mapTableExample{
    NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableCopyIn valueOptions:NSMapTableStrongMemory];
    NSObject *obj1 = [[NSObject alloc] init];
    id obj2 = [NSObject new];
    [mapTable setObject:obj1 forKey:@"obj1"];
    [mapTable setObject:obj2 forKey:@"obj2"];
}

- (void)setExample{
//    NSSet *set = [NSSet setWithObject:@"1"];
//    NSSet *set2 = [NSSet setWithObjects:@"1", nil];
//
//    NSMutableSet *__NSSetM = [NSMutableSet setWithObject:@"1"];
//    [__NSSetM addObject:nil];
//

//    NSCountedSet *countSet = [NSCountedSet setWithObject:@"1"];
//    [countSet addObject:nil];
}

- (void)hashTableExample{
    

    
    NSString *str = [NSString stringWithFormat:@"%@",@"Info.plist22"];
//    NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    ///NSRange  location=10, length=1
//     NSRange range1 = [str rangeOfCharacterFromSet:nil options:NSLiteralSearch];
//     NSString *s = @"The weather on \U0001F30D is \U0001F31E today.";
//     NSRange range =  [s              rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,100)];
//     NSString *subS = [s substringWithRange:range];
    NSString *s = [str stringByAppendingString:nil];
}

@end
